﻿using Catalogo.Portable.DataSources;
using Catalogo.Portable.Model;
using Microsoft.WindowsAzure.MobileServices;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Catalogo.Portable.Specific.DataSources
{
    public class SpecificCatalogoDataSource : CatalogoDataSource
    {
        protected override async Task<List<Categoria>> InternalObterTodasCategoriasAsync()
        {
            var mobileService = new MobileServiceClient(APPLICATION_URL, APPLICATION_KEY);
            var tabelaCategorias = mobileService.GetTable<Categoria>();
            return await tabelaCategorias.ToListAsync();
        }

        protected override async Task<List<Item>> InternalObterTodosItensAsync()
        {
            var mobileService = new MobileServiceClient(APPLICATION_URL, APPLICATION_KEY);
            var tabelaItens = mobileService.GetTable<Item>();
            return await tabelaItens.ToListAsync();
        }

        protected override async Task<Empresa> InternalObterEmpresaAsync()
        {
            var mobileService = new MobileServiceClient(APPLICATION_URL, APPLICATION_KEY);
            var tabelaEmpresas = mobileService.GetTable<Empresa>();
            return (await tabelaEmpresas.ToListAsync()).FirstOrDefault();
        }

        protected override async Task InternalInserirCategoriaAsync(Categoria categoria)
        {
            var mobileService = new MobileServiceClient(APPLICATION_URL, APPLICATION_KEY);
            var tabelaEmpresas = mobileService.GetTable<Categoria>();
            await tabelaEmpresas.InsertAsync(categoria);
        }

        protected override async Task InternalInserirItemAsync(Item item)
        {
            var mobileService = new MobileServiceClient(APPLICATION_URL, APPLICATION_KEY);
            var tabelaEmpresas = mobileService.GetTable<Item>();
            await tabelaEmpresas.InsertAsync(item);
        }

        protected override async Task InternalInserirEmpresaAsync(Empresa empresa)
        {
            var mobileService = new MobileServiceClient(APPLICATION_URL, APPLICATION_KEY);
            var tabelaEmpresas = mobileService.GetTable<Empresa>();
            await tabelaEmpresas.InsertAsync(empresa);
        }
    }
}
